import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReplaceStringInFile {
    public static List<String> readLines(InputStream inputStream) {
        List<String> lines = new ArrayList<>();
        try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine());
            }
        }
        return lines;
    }
    public static void main1(String[] args) {
        File inputFile = new File("fd/事故处理试卷.txt");
        File outputFile = new File("fd/output.txt");
        // public static String ti_reg="^\\d+\\.\\x20?";// 1. 2. 3. 

        // String oldString = "[A-Z]\\s?\\.";
        String oldString="\\x20{3}";
        String newString = "\r\n";
        try {
            List<String> lines = FileUtils.readLines(inputFile, StandardCharsets.UTF_8);
            // List<String> updatedLines = lines.stream()
            //                                  .map(line -> line.replaceAll(oldString, newString))
            //                                  .collect(Collectors.toList());
            Stream<String> a= lines.stream();
            // a=a.map(line -> line.replaceAll(oldString, newString));
            a=a.flatMap(line->Arrays.stream(line.split(oldString)));
            a=a.map(line -> line.replaceAll(" ", ""));
            a=a.map(line -> line.trim());
            a=a.filter(line->!line.isEmpty());
            // a=a.filter(lines->lines.isEmpty())

            List<String> updatedLines=a.collect(Collectors.toList());
            // List<String> updatedLines=readLines(a);

            // List<String> updatedLines=lines;


            FileUtils.writeLines(outputFile, StandardCharsets.UTF_8.name(), updatedLines);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // try {
        //     List<String> lines = FileUtils.readLines(inputFile, StandardCharsets.UTF_8);
        //     List<String> updatedLines = lines.stream()
        //                                      .map(line -> line.replace(oldString, newString))
        //                                      .collect(Collectors.toList());
        //     FileUtils.writeLines(outputFile, updatedLines, StandardCharsets.UTF_8); // 写入新文件，保持原格式（如果有需要）
    // }
    // public static void main(String[] args) {
    //     File inputFile = new File("fd/事故处理试卷.docx");
    //     File outputFile = new File("fd/output.txt");
    //     List<String> content=ReplaceAll_Str.get_list_string_from_docx(inputFile);
    //     Stream<String> a=ReplaceAll_Str.all_split_by_hashMap(content);
    //     a=ReplaceAll_Str.all_replace_by_hashMap(a);
    //     // ReplaceAll_Str.write_to_file(a, outputFile);
    //     // FileUtils.writeLines(outputFile, StandardCharsets.UTF_8.name(), updatedLines);

    }
}
